package com.example.demo.service.impl;

import com.example.demo.dao.FoodManager;
import com.example.demo.entity.Food;
import com.example.demo.service.FoodManagerService;
import com.example.demo.utils.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author cc
 * @date 2018/9/19 13:32
 */
@Service
@Transactional
public class FoodManagerServiceImpl implements FoodManagerService {
    private final FoodManager foodManager;

    @Autowired
    public FoodManagerServiceImpl(FoodManager foodManager) {
        this.foodManager = foodManager;
    }

    @Override
    public void storeFood(Food food) {
        String foodName = food.getFoodName();
        Food foodInWareHouse = foodManager.queryFoodByName(foodName);
        if (foodInWareHouse == null) {
            String foodNo = DataUtil.getRandomNo(16);
            food.setFoodNo(foodNo);
            foodManager.storeFood(food);
        } else {
            food.setFoodNo(foodInWareHouse.getFoodNo());
            foodManager.updateFoodCount(food);
        }
    }

    @Override
    public void clearFood(String foodNo) {
        if (foodNo != null) {
            foodManager.clearFood(foodNo);
        }
    }
}
